﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Entity;
using MockPrjTeam2.Biz;
namespace MockPrjTeam2.Pages.Forms
{
    public partial class DirectorateDetails : System.Web.UI.Page
    {
        int DirectorateId;
        private DirectorateBiz directorateBiz=new DirectorateBiz();
        OrganizationBuz organizationBuz=new OrganizationBuz();
        protected void Page_Load(object sender, EventArgs e)
        {
            string script =
             @"<script>
              function OpenPopupBussiness() {
                popup('../Views/PopupSearchBussiness.aspx');
            }
                 function OpenPopup() {
                popup('PopupListContacts.aspx');
            }
              function OpenPopupAddress() {
                popup('../Views/PopupSearchAddress.aspx');
            }
            function popup(url) {

                var width = 500;
                var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) { newwin.focus() }
                return false;
            }
            </script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                                                        "Mast", script);
            }

            if(!IsPostBack)
            {
                GetDirectorateByDirecId();
                GetValue();
            }
          
        }
        private void GetCountry(int countryId)
        {

            var countryBiz = new CountryBiz();
            DropDownListNationCounty.DataSource = countryBiz.GetAllCountry();
            DropDownListNationCounty.DataTextField = "CountryName";
            DropDownListNationCounty.DataValueField = "CountryID";
            DropDownListNationCounty.DataBind();
            DropDownListNationCounty.SelectedValue = countryId.ToString(CultureInfo.InvariantCulture);
        }
        private void GetDirectorateByDirecId()
        {
         // DirectorateBiz  bus=new DirectorateBiz();
            if (int.TryParse(Request.Params["directorateId"], out DirectorateId))
            {
                Directorate directorate = directorateBiz.GetDirectorateByDirectorareId(DirectorateId);

                TextBoxDirectorateName.Text = directorate.DirectorateName;
                TextBoxShortDescription.Text = directorate.DirectorateShortDescription;
                txtContact.Text = directorate.ContactName;
                TextBoxAddressLine1.Text = directorate.AddressLine1;
                TextBoxAddressLine2.Text = directorate.AddressLine2;
                TextBoxAddressLine3.Text = directorate.AddressLine3;
                txtPostCode.Text = directorate.PostCode;
                TextBoxCityTown.Text = directorate.CityTown;
                TextBoxCounty.Text = directorate.County;
                txtBussinessName.Text = directorate.BussinessTypeName;
                TxtSicCode.Text = directorate.SicCode;
                TextBoxFullDescription.Text = directorate.DirectorateFullDescription;
                TextBoxPhoneNumber.Text = directorate.PhoneNumber;
                TextBoxFax.Text = directorate.Fax;
                TextBoxEmail.Text = directorate.Email;
                TextBoxWebAddress.Text = directorate.WebAddress;
                TextBoxCharityNumber.Text = directorate.CharityNumber;
                TextBoxCompanyNumber.Text = directorate.CompanyNumber;
                this.GetCountry(directorate.CountryId);
                hdnContactId.Value = directorate.ContactId.ToString(CultureInfo.InvariantCulture);
                hdnAddressId.Value = directorate.AddressId.ToString(CultureInfo.InvariantCulture);
                hdnBussId.Value = directorate.BusinessTypeId.ToString(CultureInfo.InvariantCulture);
                HiddenFieldOrganizationId.Value = directorate.OrgId.ToString(CultureInfo.InvariantCulture);
                HiddenFieldDirectorateId.Value = directorate.DirectorateId.ToString(CultureInfo.InvariantCulture);
                hdnDirectorateName.Value = directorate.DirectorateName;
            }
        }
        private void GetValue()
        {
                OrganizationBuz organizationBuz = new OrganizationBuz();
                int OrgId =Convert.ToInt32(HiddenFieldOrganizationId.Value);
                Organization organization = organizationBuz.GetOrganizationById(OrgId);
                txtBussinessName.Text = organization.BussinessType;
                TxtSicCode.Text = organization.SicCode;
                TextBoxWebAddress.Text = organization.WebAddress;
                HiddenFielAddress.Value = organization.AddressLine1;
                HdnAddressLine2.Value = organization.AddressLine2;
                HdnAddressLine3.Value = organization.AddressLine3;
                hdnBussId.Value = Convert.ToString(organization.BusinessTypeId);
            
        }
      
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            int organisationId=Convert.ToInt32(HiddenFieldOrganizationId.Value);
            
                Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/OrganizationDetails.aspx?id={0}#DetailsPanel06", organisationId));
            
        }

        protected void ButtonLookup_Command(object sender, CommandEventArgs e)
        {

        }

        protected void CheckBoxCopy_CheckedChanged(object sender, EventArgs e)
        {

            var orgId = 0;
            if (CheckBoxCopy.Checked)
            {
   
                    TextBoxAddressLine1.Text = HiddenFielAddress.Value;
                    TextBoxAddressLine2.Text = HdnAddressLine2.Value;
                    TextBoxAddressLine3.Text = HdnAddressLine3.Value;



                
            }
        }
        public int CheckOrganizationNameExist(string drName)
        {
            
            return directorateBiz.CheckDirectorateNameExist(drName);
        }
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
          
            Directorate directorate = new Directorate();
            directorate.OrgId = Int32.Parse(HiddenFieldOrganizationId.Value);
            int id = Int32.Parse(HiddenFieldOrganizationId.Value);
            int directorateName=CheckOrganizationNameExist(TextBoxDirectorateName.Text);
            if(directorate.DirectorateName!=null&&(!directorate.DirectorateName.Equals(hdnDirectorateName.Value)&&directorateName>0))
            {
                LabelNameExists.Text = "DirectorateName does exist ! Please input new DirectorateName !";
                return;
                
            }
            directorate.DirectorateName = TextBoxDirectorateName.Text;
            directorate.DirectorateShortDescription = TextBoxShortDescription.Text;
            directorate.ContactId = Int32.Parse(hdnContactId.Value);
            directorate.AddressLine1 = TextBoxAddressLine1.Text;
            directorate.AddressLine2 = TextBoxAddressLine2.Text;
            directorate.AddressLine3 = TextBoxAddressLine3.Text;
            directorate.AddressId = Int32.Parse(hdnAddressId.Value);
            directorate.CityTown = TextBoxCityTown.Text;
            directorate.County = TextBoxCounty.Text;
            directorate.CountryId = Convert.ToInt32(DropDownListNationCounty.SelectedValue);
            directorate.BusinessTypeId = Int32.Parse(hdnBussId.Value);

            directorate.DirectorateFullDescription = TextBoxFullDescription.Text;
            directorate.PhoneNumber = TextBoxPhoneNumber.Text;
            directorate.Fax = TextBoxFax.Text;
            directorate.Email = TextBoxEmail.Text;
            directorate.WebAddress = TextBoxWebAddress.Text;
            directorate.CharityNumber = TextBoxCharityNumber.Text;
            directorate.CompanyNumber = TextBoxCompanyNumber.Text;
            directorate.DirectorateId =Convert.ToInt32(HiddenFieldDirectorateId.Value);
            bool result = directorateBiz.UpdateDirectorate(directorate);
            if (result)
            {


                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "OrganizationDetails.aspx?id={0}#DetailsPanel06", id));
            }
            else
            {
                LabelNameExists.Text = "DirectorateName does exist ! Please input new DirectorateName !";
            }
        }

        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            var directorateId = int.Parse(HiddenFieldDirectorateId.Value, CultureInfo.CurrentCulture);
            directorateBiz.SetDirectorateInActive(directorateId, false);
            ButtonInactive.Visible = false;
        }

        }
    }

